from django import forms

from web.forms.base_form import BaseForm
from web import models


class IssuesModelForm(BaseForm, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_datetime']
        widgets = {
            "assign": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            "attention": forms.SelectMultiple(
                attrs={'class': "selectpicker", "data-live-search": "true", "data-actions-box": "true"}),
            "parent": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            "start_date": forms.DateTimeInput(attrs={'autocomplete': "off"}),
            "end_date": forms.DateTimeInput(attrs={'autocomplete': "off"})
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        # 初始化所有的问题类型
        self.fields["issues_type"].choices = models.IssuesType.objects.filter(
            project=self.request.tracer.project).values_list("id", "title")



        # 初始化所有模块
        module_list = [("", "没有选中任何项")]
        module_object_list = models.Module.objects.filter(project=self.request.tracer.project).values_list("id",
                                                                                                           "title")
        module_list.extend(module_object_list)
        self.fields["module"].choices = module_list

        # 初始化指派者和关注着
        # 找到当前项目的参与者和创建者
        total_assign_list = [(self.request.tracer.project.creator_id, self.request.tracer.project.creator.username)]
        project_user_list = models.ProjectUser.objects.filter(project=self.request.tracer.project).values_list(
            "user_id", "user__username")

        total_assign_list.extend(project_user_list)
        self.fields["assign"].choices = [("", "没有选中任何项")] + total_assign_list
        self.fields["attention"].choices = total_assign_list

        # 初始化父问题
        parent_list = [("", "没有选中任何项")]
        parent_object_list = models.Issues.objects.filter(project=self.request.tracer.project).values_list("id",
                                                                                                           "subject")
        parent_list.extend(parent_object_list)
        self.fields["parent"].choices = parent_list


class IssuesReplyModelForm(forms.ModelForm):
    class Meta:
        model = models.IssuesReply
        fields = ["content", "reply"]


class InviteModelForm(BaseForm, forms.ModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ["period", "count"]
